package com.template.service.impl;



import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.template.common.ServerResponse;
import com.template.model.User;
import com.template.dao.UserMapper;

import com.template.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.Date;
import java.util.List;

/**
 * Title:  UserServiceImpl
 * Description: 请填写描述信息
 * @author yls
 * @created  2020年06月10日 11:19:45 星期三
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper  userMapper;

    @Override
    public ServerResponse detail(String id) {
        User user=userMapper.selectByPrimaryKey(id);
        return ServerResponse.createBySuccess(user);
    }

    @Override
    public ServerResponse list() {
        List<User> list=userMapper.selectAll();
        return ServerResponse.createBySuccess(list);
    }

    @Override
    public ServerResponse pageList(Integer pageNumber, Integer pageSize) {
           PageHelper.startPage(pageNumber,pageSize);
           List<User> list=userMapper.selectAll();
           PageInfo pageResult=new PageInfo(list);
           return ServerResponse.createBySuccess(pageResult);
    }

    @Override
    public ServerResponse save(User  user) {
             //创建更新时间
             userMapper.insert(user);
             return ServerResponse.createBySuccess("保存成功");
    }

    @Override
    public ServerResponse update(User  user) {
            User  current=userMapper.selectByPrimaryKey(user.getId());
            if(current==null){
                return ServerResponse.createByErrorMessage("信息不存在");
            }
            userMapper.updateByPrimaryKey(user);
            return ServerResponse.createBySuccess("更新成功");
    }

    @Override
    public ServerResponse deleteById(String id) {
          User  current=userMapper.selectByPrimaryKey(id);
          if(current==null){
            return ServerResponse.createByErrorMessage("信息不存在");
          }
          userMapper.deleteByPrimaryKey(id);
          return ServerResponse.createBySuccess("删除成功");
    }
}
